//
//  LiveCourseListVC.m
//  ZhiBoYuan
//
//  Created by JW on 2018/4/22.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "LiveCourseListVC.h"

#import "AdetailViewController.h"
#import "ABCell.h"
#import "MicroCourseModel.h"

//#import "UITableView+XY.h"
//#import "XYNoDataView.h"

//#import "EmptyView.h"
//#import "UITableView+EmptyDataTableView.h"
static NSString * identB = @"ABCell";



@interface LiveCourseListVC ()<UITableViewDataSource,UITableViewDelegate>

//@property (nonatomic, strong) UITableView *tableView;
@property(nonatomic,strong)AxcEmptyDataTableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, assign)int page;

@property(nonatomic,strong)NSMutableDictionary *shaiXuanDic;

@end

@implementation LiveCourseListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    self.view.backgroundColor = RandomColor;
    
    _page = 1;
    
    [self initWithViews];
    
    [self downloadLiveCourseDatas];
    
    [self getnote];
    
//    self.tableView.axcUI_placeHolderView = self.placeholderView;
//    
//    // 占位View的“重新加载”按钮的Block回调:
//    __weak typeof(self) WeakSelf = self;
//    self.placeholderView.clickLoadBtnBlock = ^{
//        WeakSelf.dataArray = [NSMutableArray array];
//        [WeakSelf.tableView reloadData];
//        
//        NSLog(@"刷新啊啊啊啊啊   ");
//    };
}
//#pragma mark - 懒加载区
//- (EmptyView *)placeholderView{
//    if (!_placeholderView) {
//        _placeholderView = [[EmptyView alloc] init];
//        _placeholderView.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
//    }
//    return _placeholderView;
//}

//接受通知页面方法

-(void)getnote
{
    //注册通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tongzhi:) name:@"AB" object:nil];
}

- (void)tongzhi:(NSNotification *)text
{
    
    NSLog(@"AB =%@",text.userInfo);
    self.shaiXuanDic= [NSMutableDictionary dictionary];
    
    self.shaiXuanDic = text.userInfo.mutableCopy;
    [self downloadLiveCourseDatas];
    
}

-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"AB"  object:nil];
}


- (void)downloadLiveCourseDatas{
    
    
    SGParameter *parameter  = [[SGParameter alloc] init];
    parameter.body = @{
                       @"page":[NSString stringWithFormat:@"%d",_page],
                       @"page_count":@"10",
                        @"course_type":@"1",// 1 直播 2微课 3套餐
                        @"list_type"  :self.listType
                       };
    
    
    
    
    if ([self.shaiXuanDic allKeys].count > 0) {
        ShaiXuan *s = [[ShaiXuan alloc]initWithDictionary:self.shaiXuanDic];
        
        
        
        parameter.body = @{
                           @"xueduan_id":s.xueDuan,
                           @"xueke_id":s.xueKe,
                           @"nianji_id":s.nianJi,
                           @"knowledge_point_id":s.zhiShiDian,
                           @"is_free":s.leiXing,
                           @"list_type":self.listType,
                           @"page":[NSString stringWithFormat:@"%d",_page],
                           @"page_count":@"10",
                           @"course_type":@"2"// 1 直播 2微课 3套餐
                           };
        
    }
    
    
    
    
    [SGHttpTool POST:Host_Append(CourseList) parameter:parameter sucess:^(id responseObject) {
        ZYLog(@"%@",responseObject);
        
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        
        if (_page == 1) {
            [_dataArray removeAllObjects];
        }
        
        
        SGReceive *receive = [SGReceive receiveWithResponseObject:responseObject dataWord:@"course_list" dataClass:[MicroCourseModel class]];
//        [self.dataArray addObjectsFromArray:receive.data];
        for (MicroCourseModel *model in receive.data) {
            [_dataArray addObject:model];
        }
        
        if (_dataArray.count < 4) {
            _tableView.mj_footer.hidden = YES;
        }else{
            _tableView.mj_footer.hidden = NO;
        }
        
        
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
        
    }];
    
}

#pragma mark -- UITableViewDataSource,UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ABCell *cell = [tableView dequeueReusableCellWithIdentifier:identB forIndexPath:indexPath];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor clearColor];
    cell.microModel = self.dataArray[indexPath.row];
    cell.Kebadage.hidden = YES;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
//    AdetailViewController *vc= [[AdetailViewController alloc]initWithNibName:@"AdetailViewController" bundle:nil];
//    vc.course_id = [self.dataArray[indexPath.row] microCourseID];
//    vc.hidesBottomBarWhenPushed =YES;
//
//    [ZYCurrentViewController().navigationController pushViewController:vc animated:YES];
    
    
     [Helper keChengDetailFromVC:ZYCurrentViewController() course_id:[self.dataArray[indexPath.row] microCourseID]];
    
}

#pragma mark - 视图
- (void)initWithViews{
    
    [self.view addSubview:self.tableView];
    self.tableView.sd_layout
    .topSpaceToView(self.view, 0)
    .leftSpaceToView(self.view, 0)
    .rightSpaceToView(self.view, 0)
    .bottomSpaceToView(self.view, 0);
    
    
}

#pragma makr -- property
- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}




- (AxcEmptyDataTableView *)tableView {
    if (!_tableView) {
        AxcEmptyDataTableView *  tableView = [[AxcEmptyDataTableView alloc] initWithFrame:CGRectMake(0, 0, ScreenHeight, ScreenHeight- NAVIGATION_BAR_HEIGHT - TAB_BAR_HEIGHT - 45) style:UITableViewStylePlain];
        NSLog(@"%f-%@",ScreenHeight,NSStringFromCGRect(self.view.frame));
        tableView.backgroundColor = [UIColor clearColor];
        tableView.delegate        = self;
        tableView.dataSource      = self;
//        tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        
        tableView.estimatedRowHeight = 44.0f;//推测高度，必须有，可以随便写多少
        
        tableView.rowHeight = UITableViewAutomaticDimension;//iOS8之后默认就是这个值，可以省略
        
        //        self.extendedLayoutIncludesOpaqueBars = YES;
        
        if (@available(iOS 11.0, *)) {
            tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        } else {
            self.automaticallyAdjustsScrollViewInsets = NO;
        }
//        tableView.contentInset = UIEdgeInsetsMake(0, 0, 49, 0);
//        tableView.scrollIndicatorInsets = _tableView.contentInset;
        
        [tableView registerNib:[UINib nibWithNibName:identB bundle:nil] forCellReuseIdentifier:identB];
        
        [tableView setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];
         __weak typeof(self) WeakSelf = self;
        
        // 下拉刷新
        tableView.mj_header= [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            
            [WeakSelf.shaiXuanDic removeAllObjects];
            
            _page = 1;
            
            [WeakSelf downloadLiveCourseDatas];
            
        }];
        
        // 上拉加载
        tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            
            _page++;
            
            [WeakSelf downloadLiveCourseDatas];
            
        }];
        
//        self.placeholderView.frame = tableView.bounds;
        
        // 设置空集时候展示的View
        tableView.axcUI_placeHolderView = self.placeholderView;
        
        //  默认YES 自带渐入渐出动画效果，下边这个参数可以关闭
        tableView.axcUI_placeHolderViewAnimations = NO;
        
        // 占位View的“重新加载”按钮的Block回调:
       
        
        self.placeholderView.clickLoadBtnBlock = ^(id type) {
            //        WeakSelf.dataArray = nil;
            
            
            [WeakSelf.shaiXuanDic removeAllObjects];
            
            _page = 1;
            
            [WeakSelf downloadLiveCourseDatas];
            
        };
        
        
        _tableView = tableView;
        
    }
    return _tableView;
}


#pragma mark - TableView 占位图
//- (UIImage *)xy_noDataViewImage
//{
//    return [UIImage imageNamed:@"Load_failed"];
//}

//- (NSString *)xy_noDataViewMessage
//{
//    return @"没有要展示内容.....";
//}

//- (UIColor *)xy_noDataViewMessageColor
//{
//    return [UIColor blackColor];
//}

//- (void)xy_reloadDataView
//{
//    _page = 1;
//
//    [self downloadLiveCourseDatas];
//
//}

@end
